package com.ikangtai.shecare.common.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.ikangtai.shecare.common.App;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class InternetUtil {
    /**
     * TODO 判断网络状态是否可用
     *
     * @return true: 网络可用 ; false: 网络不可用
     */
    public static boolean hasInternet() {
        ConnectivityManager m = (ConnectivityManager) App.context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (m == null) {
            return false;
        } else {
            NetworkInfo[] info = m.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * @return 是否是wifi网络
     */
    public static boolean isWifi() {
        ConnectivityManager m = (ConnectivityManager) App.context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo n = m.getActiveNetworkInfo();
        if (n != null && n.getType() == ConnectivityManager.TYPE_WIFI)
            return true;
        return false;
    }

    public static Bitmap getBitmapFromNetwork(String url, Context context) throws Exception {
        AppLog.i("hxiao 2, ThreadId="+Thread.currentThread().getId());

        BitmapFactory.Options options=new BitmapFactory.Options();
        BitmapFactory.decodeStream(getImageStream(url),null,options);
        options.inJustDecodeBounds = true;
        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        int inSampleSize = 1;

        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int screenWidth = outMetrics.widthPixels;
        int screenHeight = outMetrics.heightPixels;

        if (imageHeight > screenHeight || imageWidth > screenWidth) {
            inSampleSize = 5;
        }

        options.inJustDecodeBounds = false;
        options.inSampleSize = inSampleSize;

        return BitmapFactory.decodeStream(getImageStream(url),null,options);
    }

    /**
     * Get image from newwork
     *
     * @param urlParam The path of image  http://....
     * @return InputStream
     * @throws Exception
     */
     private static InputStream getImageStream(String urlParam) throws Exception {
         AppLog.i("hxiao 3, ThreadId="+Thread.currentThread().getId());
        URL url = new URL(urlParam);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            return conn.getInputStream();
        }
        return null;
    }
}
